package cn.stronglink.assetmanage.business.search.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;

/**
 * Created by zhengjinglan on 2018/6/19.
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> implements View.OnClickListener{

    private Context mContext;
    private List<SearchAsset> datas;
    private onRecyclerListener listener;

    public SearchAdapter(Context mContext){
        this.mContext = mContext;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_search, parent, false);
        view.setOnClickListener(this);

        return new SearchAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        SearchAsset searchAsset = datas.get(position);
        holder.bindRepo(searchAsset, position);
        holder.itemView.setTag(position);
    }



    @Override
    public int getItemCount() {
        return datas == null?0:datas.size();
    }
    public void  setOnItemClickListener(onRecyclerListener listener){
        this.listener = listener;
    }
    public void notifyDatasChanged(List<SearchAsset> data){
        this.datas = data;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (listener != null){
            listener.onItemClicked(v, (int) v.getTag());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private List<SearchAsset> data;
        private TextView tv_search_asset_name, tv_search_rfid_no,tv_status;
        private onRecyclerListener listener;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_search_asset_name = itemView.findViewById(R.id.tv_search_asset_name);
            tv_search_rfid_no = itemView.findViewById(R.id.tv_search_rfid_no);
            tv_status = itemView.findViewById(R.id.tv_status);
        }

        public void bindRepo(SearchAsset searchAsset, int position){
            tv_search_asset_name.setText(searchAsset.getAssetName());
            tv_search_rfid_no.setText(searchAsset.getSerialNo());
            switch (searchAsset.getStatus()) {
                case 1:
                    tv_status.setText("在库");
                    break;
                case 2:
                    tv_status.setText("已上架");
                    break;
                case 3:
                    tv_status.setText("报废");
                    break;
                case 4:
                    tv_status.setText("已出库");
                    break;
                case 5:
                    tv_status.setText("待验收");
                    break;
                case 6:
                    tv_status.setText("在使用");
                    break;
            }

        }
    }
}
